package com.jiandian.controller;

import com.jiandian.crudparams.dto.AddDictDto;
import com.jiandian.crudparams.dto.EditDictDto;
import com.jiandian.crudparams.swaggernotes.DictNotes;
import com.jiandian.entity.Dict;
import com.jiandian.page.PageInfo;
import com.jiandian.service.DictService;
import com.jiandian.util.BaseController;
import com.jiandian.util.BaseResult;
import com.jiandian.util.BeanUtils;
import com.jiandian.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典
 *
 * @author Houqi Fu
 * @date 2019/01/31
 */
@Api(value = "Dict", description = "字典")
@RestController
@RequestMapping(value = "${jiandian.api.version}/Dict")
public class DictController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DictController.class);

    @Autowired
    private DictService service;

    @ApiOperation(value = "添加", notes = DictNotes.ADD_NOTE)
    @GetMapping("/{key}")
    public BaseResult findByKey(@PathVariable("key")String key){
        Dict byKey = service.findByKey(key);
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,byKey);
    }


    // @ApiOperation(value = "添加", notes = DictNotes.ADD_NOTE)
    // @ApiImplicitParam(name = "record", value = DictNotes.ADD_VALUE, required = true, dataType = "AddDictDto")
    // @PostMapping("")
    // public BaseResult add(@RequestBody @Validated AddDictDto record, BindingResult result){
    //     logger.info("添加字典，参数：\n {}", ReflectionToStringBuilder.toString(record));
    //     if (result.hasErrors()) {
    //         FieldError fieldError = result.getFieldError();
    //         logger.error("添加字典参数错误:{}", fieldError.getDefaultMessage());
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
    //     }
    //     Dict bean = new Dict();
    //     BeanUtils.copyProperties(record, bean);
    //     Dict saveResult = service.add(bean);
    //     if (saveResult != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, saveResult);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }
    //
    // @ApiOperation(value = "修改", notes = DictNotes.EDIT_NOTE)
    // @ApiImplicitParam(name = "record", value = DictNotes.EDIT_VALUE, required = true, dataType = "EditDictDto")
    // @PutMapping("/{id}")
    // public BaseResult edit(@PathVariable("id") String id, @RequestBody @Validated EditDictDto record, BindingResult result){
    //     logger.info("修改字典，参数：\n {}", ReflectionToStringBuilder.toString(record));
    //     if (result.hasErrors()) {
    //         FieldError fieldError = result.getFieldError();
    //         logger.error("修改字典参数错误:{}", fieldError.getDefaultMessage());
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
    //     }
    //     Dict bean = new Dict();
    //     bean.setId(Integer.valueOf(id));
    //     BeanUtils.copyProperties(record, bean);
    //     Dict updateResult = service.update(bean);
    //     if (updateResult != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, updateResult);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }
    //
    // @ApiOperation(value = "列表", notes = DictNotes.LIST_NOTE)
    // @ApiImplicitParam(name = "pageInfo", value = DictNotes.LIST_VALUE, required = true, dataType = "PageInfo")
    // @GetMapping("")
    // public BaseResult list(Dict whereEntity, PageInfo pageInfo){
    //     logger.info("查询字典，参数：\n {}", ReflectionToStringBuilder.toString(pageInfo));
    //     PageInfo pageResult = service.getPage(whereEntity, pageInfo);
    //     return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,pageResult);
    // }
    //
    // @ApiOperation(value = "详情", notes = "字典详情")
    // @GetMapping("/{id}")
    // public BaseResult detail(@PathVariable("id") String id){
    //     logger.info("查询字典，参数：\n id = {}", id);
    //     if (StringUtils.isBlank(id)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数uuid不存在");
    //     }
    //     Dict record = service.findById(Integer.valueOf(id));
    //     if (record != null) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, record);
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "不存在");
    // }
    //
    // @ApiOperation(value = "删除", notes = "根据字典id删除")
    // @DeleteMapping("/{id}")
    // public BaseResult delete(@PathVariable("id") String id){
    //     logger.info("删除字典，参数：\n id = {}", id);
    //     if (StringUtils.isBlank(id)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数id不存在");
    //     }
    //     int deleteResult = service.delete(Integer.valueOf(id));
    //     if (deleteResult > 0) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }
    //
    // @ApiOperation(value = "批量删除", notes = "根据字典id list批量删除")
    // @PostMapping("batchDelete")
    // public BaseResult batchDelete(@RequestBody List<Integer> ids){
    //     logger.info("批量删除字典，参数：\n {}", ReflectionToStringBuilder.toString(ids, ToStringStyle.MULTI_LINE_STYLE));
    //     if (CollectionUtils.isEmpty(ids)) {
    //         return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "参数ids不存在");
    //     }
    //     int deleteResult = service.batchDeleteById(ids);
    //     if (deleteResult > 0) {
    //         return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200, "删除成功");
    //     }
    //     return sendResult(Constants.RESPONSE_CODE_400,Constants.RESPONSE_MESSAGE_400, "系统异常");
    // }

}
